package cn.tedu.charging.order.producer;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
@Slf4j
public class OrderEmqxProducer {
    @Autowired
    private MqttClient mqttClient;
    //根据充电桩id 组织一个主题
    //将消息对象 序列化成byte发送消息
    public boolean sendMessage(String topic,String message){
        log.info("订单要向主题:{},发送消息:{}",topic,message);
        try{
            mqttClient.publish(
                    topic,message.getBytes(StandardCharsets.UTF_8),
                    0,false);
        }catch (Exception e){
            log.error("订单向主题:{},发送消息:{}失败",topic,message,e);
            return false;
        }
        return true;
    }
}
